/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.FaceDesign;
import com.yami.shop.bean.enums.DesignStatusEnum;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.FaceDesignService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;


/**
 * 设计稿管理控制器
 * @author
 */
@RestController
@RequestMapping("/admin/faceDesign")
public class FaceDesignController {

    @Autowired
    private FaceDesignService faceDesignService;

    /**
     * 分页获取设计稿列表
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesign:page')")
    public ServerResponseEntity<IPage<FaceDesign>> page(FaceDesign faceDesign, PageParam<FaceDesign> page) {
        IPage<FaceDesign> faceDesignPage = faceDesignService.page(page, new LambdaQueryWrapper<FaceDesign>()
                .like(StrUtil.isNotBlank(faceDesign.getDesignName()), FaceDesign::getDesignName, faceDesign.getDesignName())
                .eq(faceDesign.getUserId() != null, FaceDesign::getUserId, faceDesign.getUserId())
                .eq(faceDesign.getDraftId() != null, FaceDesign::getDraftId, faceDesign.getDraftId())
                .eq(faceDesign.getDesignStatus() != null, FaceDesign::getDesignStatus, faceDesign.getDesignStatus()));
        return ServerResponseEntity.success(faceDesignPage);
    }

    /**
     * 获取设计稿详情
     */
    @GetMapping("/info/{designId}")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesign:info')")
    public ServerResponseEntity<FaceDesign> info(@PathVariable("designId") String designId) {
        FaceDesign faceDesign = faceDesignService.getById(designId);
        return ServerResponseEntity.success(faceDesign);
    }

    /**
     * 新增设计稿
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceDesign:save')")
    public ServerResponseEntity<Void> save(@RequestBody FaceDesign faceDesign) {
        faceDesign.setCreateTime(new Date());
        faceDesign.setUpdateTime(new Date());
        faceDesignService.save(faceDesign);
        return ServerResponseEntity.success();
    }

    /**
     * 修改设计稿
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceDesign:update')")
    public ServerResponseEntity<Void> update(@RequestBody FaceDesign faceDesign) {
        faceDesign.setUpdateTime(new Date());
        faceDesignService.updateById(faceDesign);
        return ServerResponseEntity.success();
    }

    /**
     * 删除设计稿
     */
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceDesign:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody String[] designIds) {
        faceDesignService.removeByIds(Arrays.asList(designIds));
        return ServerResponseEntity.success();
    }

    /**
     * 更新设计稿状态
     */
    @PutMapping("/status")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesign:update')")
    public ServerResponseEntity<Void> updateStatus(@RequestBody FaceDesign faceDesign) {
        FaceDesign updateFaceDesign = new FaceDesign();
        updateFaceDesign.setDesignId(faceDesign.getDesignId());
        updateFaceDesign.setDesignStatus(faceDesign.getDesignStatus());
        updateFaceDesign.setUpdateTime(new Date());
        faceDesignService.updateById(updateFaceDesign);
        return ServerResponseEntity.success();
    }
}
